package com.th.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.th.common.domain.UsSessionToken;
import com.th.common.domain.UsUser;
import com.th.common.exception.BaseServiceException;
import com.th.common.mybatis.query.LambdaQueryWrapperX;
import com.th.common.vo.UserInfoVo;
import com.th.mapper.UsUserMapper;
import com.th.service.UsSessionTokenService;
import com.th.service.UsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @Author tuhang
 * @PackageName nswcy-crowdsource-client-server
 * @Package com.th.service.impl
 * @Date 2023/6/6 20:51
 * @Version 1.0
 */
@Service
public class UsUserServiceImpl extends ServiceImpl<UsUserMapper, UsUser> implements UsUserService  {


    @Resource
    private UsSessionTokenService usSessionTokenService;


    @Override
    public String login(String username, String password) {
        UsUser usUser = this.getBaseMapper().selectOne(UsUser::getUserName, username);

        if(Objects.isNull(usUser)){
            throw new BaseServiceException(500,"当前用户名不存在！！！");
        }
        if (!usUser.getPassWord().equals(password)) {
            throw new BaseServiceException(500,"当前用户密码错误，请重新登录！！！");
        }
        if (usUser.getStatus() != 0) {
            throw new BaseServiceException(500,"当前用户被禁用，请联系网站人员！！！");
        }

        return usSessionTokenService.createToken(usUser.getId());
    }

    @Override
    public UserInfoVo getUserInfo(String token) {
        UsSessionToken usSessionToken = usSessionTokenService.getToken(token);
        UsUser usUser = this.getBaseMapper().selectOne(UsUser::getId, usSessionToken.getUserId());

        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setAvatar(usUser.getUserAvatar());
        userInfoVo.setNickName(usUser.getNickName());
        userInfoVo.setRoleType(usUser.getUserRole());
        return userInfoVo;
    }


    @Override
    public Boolean logout(String token) {
        UsSessionToken usSessionToken = usSessionTokenService.getToken(token);
        if(Objects.nonNull(usSessionToken)){
            usSessionTokenService.removeByUserId(usSessionToken.getUserId());
        }
        return true;
    }

    @Override
    public Long getCurrentUserId() {
        HttpServletRequest request = getRequest();
        String token = request.getHeader("token");
        UsSessionToken usSessionToken = usSessionTokenService.getToken(token);
        return usSessionToken.getUserId();
    }


    /**
     * 获得请求
     *
     * @return HttpServletRequest
     */
    public static HttpServletRequest getRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (!(requestAttributes instanceof ServletRequestAttributes)) {
            return null;
        }
        return ((ServletRequestAttributes) requestAttributes).getRequest();
    }

}
